package com.apes.scm.settle.settle.model;

import com.apes.framework.jpa.BaseModel;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.util.ApesConst;
import com.apes.scm.masterdata.finance.model.BankAccount;
import com.apes.scm.masterdata.finance.model.PaymentMethodType;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import javax.validation.constraints.NotNull;

@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = SettleSimplePaymentMethod.CONST_TABLE_NAME)
public class SettleSimplePaymentMethod extends BaseModel {
    public static final String CONST_TABLE_NAME = "acc_settle_simple_payment";

    @Id
    @Column(name = "id", length = 32)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "支付明细", prefix = "FI")
    @Comment("付款方式编码")
    private String id;

    @NotNull
    @ManyToOne(optional = false)
    @JoinColumn(name = "settle_simple_id", referencedColumnName = "id")
    @Comment("付款单编码")
    private SettleSimple settleSimple; /*付款主表*/

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name="payment_method_type", referencedColumnName="id", nullable = false)
    @Comment("付款方式")
    private PaymentMethodType paymentMethodType;  //付款方式

    @Column(name = "amount", nullable = false, columnDefinition = ApesConst.FIELD_16_4)
    @Comment("付款金额")
    private double amount = 0; //付款金额

    @Column(name = "note", length = 250)
    @Comment("摘要")
    private String note; //备注

    @Comment("银行账户")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "bankAccount_id", referencedColumnName = "id")
    private BankAccount bankAccount;

    @Transient
    @Comment("银行帐号")
    private String bankAccountId;

    public String getBankAccountId(){
        String code = null;
        if (bankAccount != null) {
            code = bankAccount.getAccountId();
        }
        return code;
    }

}
